<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Libs\Filter\Filter;
use App\Models\MasterCash;
use App\Models\MasterFund;
use App\Services\IFinanceService;
use App\Services\ISystemService;
use Illuminate\Http\Request;

/**
 * 财务管理
 * Class FinanceController
 * @package App\Http\Controllers
 */
class FinanceController extends BaseController
{

    /**
     * 商家流水
     * @param Request $request
     */
    public function merchantTrade(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('用户名：')
            ->input('merchant_name')
            ->HTML('涉及订单号：')
            ->input('order_serial_number_list')
            ->HTML('流水号：')
            ->input('trade_number')
            ->HTML('交易类型：')
            ->select('type',array(''=>'请选择')+Params::$MERCHANT_TRADE_TYPE,null,array('style'=>'width:150px'))
            ->br()->br()
            ->HTML('交易时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->merchantTradeList($filter->get_where());
        }else{
            $list = $financeService->merchantTradeList();
        }
        return view('finance/merchantTrade',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 师傅流水
     * @param Request $request
     */
    public function masterTrade(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
        ->HTML('师傅姓名：')
        ->input('master_name')
        ->HTML('涉及订单号：')
        ->input('order_serial_number')
        ->HTML('流水号：')
        ->input('trade_number')
        ->br()->br()
        ->HTML('交易时间：')
        ->date('add_time')
        ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->masterTradeList($filter->get_where());
        }else{
            $list = $financeService->masterTradeList();
        }
        return view('finance/masterTrade',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 质保金流水
     * @param Request $request
     */
    public function masterFundTrade(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('师傅姓名：')
            ->input('master_name')
            ->HTML('流水号：')
            ->input('trade_number')
            ->br()->br()
            ->HTML('缴纳时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->masterFundTradeList($filter->get_where());
        }else{
            $list = $financeService->masterFundTradeList();
        }
        return view('finance/masterFundTrade',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 质保金待审核退还
     * @param Request $request
     */
    public function masterFundTradeReturning(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('师傅姓名：')
            ->input('master_name')
            ->HTML('流水号：')
            ->input('trade_number')
            ->br()->br()
            ->HTML('申请时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->masterFundTradeReturningList($filter->get_where());
        }else{
            $list = $financeService->masterFundTradeReturningList();
        }
        return view('finance/masterFundTradeReturning',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 质保金已审核退还
     * @param Request $request
     */
    public function masterFundTradeReturned(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('师傅姓名：')
            ->input('master_name')
            ->HTML('流水号：')
            ->input('trade_number')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->masterFundTradeReturnedList($filter->get_where());
        }else{
            $list = $financeService->masterFundTradeReturnedList();
        }
        return view('finance/masterFundTradeReturned',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 师傅提现审核
     * @param Request $request
     */
    public function masterCash(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('师傅姓名：')
            ->input('master_name')
            ->HTML('流水号：')
            ->input('trade_number')
            ->br()->br()
            ->HTML('申请时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->masterCashReviewList($filter->get_where());
        }else{
            $list = $financeService->masterCashReviewList();
        }
        return view('finance/masterCash',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 师傅提现已审核
     * @param Request $request
     */
    public function masterCashed(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('师傅姓名：')
            ->input('master_name')
            ->HTML('流水号：')
            ->input('trade_number')
            ->br()->br()
            ->HTML('放款时间：')
            ->date('upd_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->masterCashPassList($filter->get_where());
        }else{
            $list = $financeService->masterCashPassList();
        }
        return view('finance/masterCashed',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 师傅提现已拒绝
     * @param Request $request
     */
    public function masterCashRefuse(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('师傅姓名：')
            ->input('master_name')
            ->HTML('流水号：')
            ->input('trade_number')
            ->br()->br()
            ->HTML('放款时间：')
            ->date('upd_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->masterCashRefuseList($filter->get_where());
        }else{
            $list = $financeService->masterCashRefuseList();
        }
        return view('finance/masterCashRefuse',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 拒绝提现
     * @param Request $request
     * @param IFinanceService $financeService
     */
    public function cashDisagree(MasterCash $cash,Request $request,IFinanceService $financeService)
    {
        $financeService->cashDisagree($cash);
        return back();
    }

    /**
     * 同意提现/批量提现
     * @param Request $request
     * @param IFinanceService $financeService
     */
    public function cashAgree($cash_id = null,Request $request,IFinanceService $financeService,ISystemService $systemService)
    {
        if ($cash_id){
            $ret = $financeService->cashAgree($cash_id);
        }else if ($request->has('cash_ids')){
            $ret = $financeService->cashAgreeMutil((array)$request->input('cash_ids'));
        }
        if (!empty($ret)){
            $systemService->success('放款成功');
        }else{
            $systemService->alert('放款失败');
        }
        return back();
    }

    /**
     * 拒绝退还质保金
     * @param Request $request
     * @param IFinanceService $financeService
     */
    public function returnFundDisagree(MasterFund $fund,Request $request,IFinanceService $financeService)
    {
        $financeService->returnFundDisagree($fund);
        return back();
    }

    /**
     * 同意退还/批量退还质保金
     * @param Request $request
     * @param IFinanceService $financeService
     */
    public function returnFundAgree($fund_id = null,Request $request,IFinanceService $financeService,ISystemService $systemService)
    {
        if ($fund_id){
            $ret = $financeService->returnFundAgree($fund_id);
        }else if ($request->has('fund_ids')){
            $ret = $financeService->returnFundAgreeMutil((array)$request->input('fund_ids'));
        }
        if (!empty($ret)){
            $systemService->success('退还成功');
        }else{
            $systemService->alert('退还失败');
        }
        return back();
    }

    /**
     * 系统钱包记录
     * @param Request $request
     */
    public function systemWalletHistory(Request $request,IFinanceService $financeService)
    {
        $filter = Filter::instance()
            ->HTML('发生时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $financeService->systemWalletHistory($filter->get_where());
        }else{
            $list = $financeService->systemWalletHistory();
        }
        return view('finance/systemWalletHistory',['list'=>$list,'filter'=>$filter]);
    }

}
